<template>
    <div class="shopcar-container" >
        <div class="mui-card" v-for="(item,i) in shopList" :key="item.id">
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <mt-switch v-model="$store.getters.selectobj[item.id]" @change="selectChange(item.id,$store.getters.selectobj[item.id])"></mt-switch>
                    <img :src="item.thumb_path" width="60" height="60">
                    <div class="info">
                        <h1>{{ item.title }}</h1>
                        <p>
                            <span class="red">{{ item.sell_price }}</span>
                            <numberbox :initcount="$store.getters.shopCount[item.id]" :shopId="item.id"></numberbox>
                            <a href="javascript:;" @click="del(item.id,i)">删除</a>
                        </p>
                    </div>
                </div>
            </div>
        </div>
        <div class="mui-card">
                <div class="mui-card-content">
                    <div class="mui-card-content-inner">
                        <div class="info">
                            <p>总计(不含运费)</p>
                            <p>已勾选商品 <span class="red_size">{{$store.getters.jiesuanG.count}}</span>件&nbsp;,&nbsp; 总价&nbsp;:&nbsp; <span class="red_size">￥{{$store.getters.jiesuanG.sum}}</span></p>
                        </div>
                        <mt-button type="danger">去结算</mt-button>
                    </div>
                </div>
            </div>
    </div>
</template>
<script>
// 导入numberbox组件
import numberbox from "../subcomponents/shop_numberbox.vue";
export default {
    data(){
        return{
            shopList:[],
            jiesuanD:{}
        }
    },
    created(){
        this.getShopData();
    },
    methods:{
        getShopData(){
            // 根据文档中的介绍，首先要拿到stora中所有的id,最后用，拼接
            var idArr=[];
            this.$store.state.car.forEach( item => {
                idArr.push(item.id);
            });
            //如果本来就没有id，就不发起请求
            if(idArr.lenth<=0){
                return true;
            }
            // console.log(idArr.join(","));//join可以用来进行字符串的拼接
            this.$http.get("http://www.liulongbin.top:3005/api/goods/getshopcarlist/"+idArr.join(",")).then(result => {
                if(result.body.status===0){
                    // console.log(result.body);
                    this.shopList=result.body.message;
                }
            })
        },
        del(id,index){
            this.shopList.splice(index,1);
            this.$store.commit("delM",id);
        },
        selectChange( id, val ){
            // console.log(id +"---"+ val);
            this.$store.commit("updataSelected", {"id":id, "selected":val} );//构造对象要规范
        },
    },
    components:{
        numberbox,
    }
}

</script>
<style lang="less" scoped>
.shopcar-container{
    background-color: #eee;
    overflow:hidden;
    .mui-card-content-inner{
        display: flex;
        justify-content: space-between;
        align-items:center;
        .info{
            // dispaly:flex;
            // flex-direction:column;
            h1{
                font-size:14px;
            }
            p{
                margin-buttom:0px;
                .red{
                font-size:12px;
                color:red;
                font-weight: bold;
                }
                a{
                    font-size:12px;
                }
            }
        }
        .red_size{
            color :red;
            font-size:16px;
            font-weight: bold;
        }

    }

}
</style>